package forms;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;



public class AddressFormInsert {
	private static final long serialVersionUID = 1L;
	
	private String street = null;
	private String city = null;
	private String state = null;
	private String zipCode = null;
	private String countryCode = null;
	private String areaCode = null;
	private String telephone = null;

	
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	public String getCountryCode() {
		return countryCode;
	}
	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}
	public String getAreaCode() {
		return areaCode;
	}
	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setStreet(null);
		setCity(null);
		setState(null);
		setZipCode(null);
		setCountryCode(null);
		setAreaCode(null);
		setTelephone(null);
	}
	
	public Boolean checkNumber(String s){
		boolean isValid = false; 
		String expression = "^[0-9 \\- \\s]*$";  
		CharSequence inputStr = s;  
		Pattern pattern = Pattern.compile(expression);  
		Matcher matcher = pattern.matcher(inputStr);  
		if(matcher.matches()){  
		isValid = true;  
		}  
		return isValid;  
	}
	
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {

		Boolean isUS = request.getSession().getAttribute("residence").equals("United States");
		
		ActionErrors errors = new ActionErrors();
		
		if ((street == null) || (street.length() < 1))
			errors.add("streetMsgTag1", new ActionMessage("errors.required",
					"Street"));

		if ((city == null) || (city.length() < 1))
			errors.add("cityMsgTag1", new ActionMessage("errors.required",
					"City"));

		if (isUS && ((state == null) || (state.length() < 1)))
			errors.add("stateMsgTag1", new ActionMessage("errors.required",
					"State"));
		
		if(!checkNumber(zipCode)){
			errors.add("zcodeMsgTag1", new ActionMessage("errors.code",
			"ZipCode"));
		}
		
		if(!isUS && !checkNumber(countryCode)){
			errors.add("ccodeMsgTag1", new ActionMessage("errors.code",
			"CountryCode"));
		}
		
		if(!checkNumber(areaCode)){
			errors.add("acodeMsgTag1", new ActionMessage("errors.code",
			"AreaCode"));
		}
		
		if(!checkNumber(telephone)){
			errors.add("teleMsgTag1", new ActionMessage("errors.code",
			"Telephone Number"));
		}
		
		return errors;
	}
}
